//
//  UIBarButtonItem+Extension.m
//  DADReadTool
//
//  Created by wanjiading on 15/5/30.
//  Copyright (c) 2015年 DAD. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)

/**
 *  创建一个拥有2张图片的item
 *
 *  @param image     普通图片
 *  @param highImage 高亮图片
 *  @param target    点击item后会调用target的action方法
 *  @param action    点击item后会调用target的action方法
 */
+ (instancetype)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.frame = (CGRect){CGPointZero, button.currentImage.size};
    return [[self alloc] initWithCustomView:button];
}

/**
 *  创建一个拥有2张图片的item
 *
 *  @param image     普通图片
 *  @param selectedImage     选中状态图片
 *  @param target    点击item后会调用target的action方法
 *  @param action    点击item后会调用target的action方法
 */
+ (instancetype)itemWithImage:(NSString *)image selectedImage:(NSString *)selectedImage target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.frame = (CGRect){CGPointZero, button.currentImage.size};
    return [[self alloc] initWithCustomView:button];
}

/**
 *  快速创建一个按钮
 */
+ (instancetype)itemWithTitle:(NSString *)title target:(id)target action:(SEL)action {
    return [self itemWithTitle:title fontSize:[UIFont boldSystemFontOfSize:16] target:target action:action];
}

+ (instancetype)itemWithTitle:(NSString *)title fontSize:(UIFont *)font target:(id)target action:(SEL)action {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn.titleLabel setFont:font];
    [btn sizeToFit];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[self alloc] initWithCustomView:btn];
}

@end
